package com.dodo.blog.ui.page.publish;

import com.dodo.blog.model.Article;
import com.dodo.blog.request.ArticlesRequest;
import com.dodo.blog.server.ArticleService;
import com.dodo.blog.ui.component.container.H2;
import com.dodo.blog.ui.component.container.Paragraph;
import com.dodo.blog.ui.component.repeater.IDataSource;
import com.dodo.blog.ui.component.repeater.OrderDirection;
import com.dodo.blog.ui.component.repeater.Repeater;
import com.dodo.blog.ui.page.AbstractPage;
import com.dodo.blog.ui.util.UriConstructor;

import javax.inject.Inject;
import java.util.List;

/**
 * <p>This page maps these urls:</p>
 * <ul>
 * <li>/articles</li>
 * <li>/articles/id/1234/google-app-engine-tutorial</li>
 * <li>/articles/date/2011</li>
 * <li>/articles/date/2011/03</li>
 * <li>/articles/date/2011/03/03</li>
 * <li>/articles/categories/frontend</li>
 * <li>/articles/tags/css3</li>
 * </ul>
 *
 * @author <a href="mailto:pohorelec@comvai.com">Jozef Pohorelec</a>
 */
public class Articles
        extends AbstractPage
{
    private static final long serialVersionUID = 1L;

    private static ArticleService articleService;

    @Override
    public void onRender()
    {
        String[] path = getRequest().getServletPath().split( "/" );

        if ( path.length > 3 )
        {
            String subPath = path[2];
            if ( subPath.equals( UriConstructor.PARAM_ARTICLE_ID ) )
            {
                String id = path[3];
                renderArticle( id );
            }
            else if ( subPath.equals( UriConstructor.PARAM_ARTICLE_CATEGORY ) )
            {
                String category = path[3];

                ArticlesDataSource dataSource = new ArticlesDataSource();
                dataSource.searchByCategory( category );
                renderArticles( dataSource );
            }
            else if ( subPath.equals( UriConstructor.PARAM_ARTICLE_TAG ) )
            {
                String tag = path[3];

                ArticlesDataSource dataSource = new ArticlesDataSource();
                dataSource.searchByTag( tag );
                renderArticles( dataSource );
            }
            else if ( subPath.equals( UriConstructor.PARAM_ARTICLE_DATE ) )
            {
                String year = null;
                String month = null;
                String day = null;

                if ( path.length > 3 )
                {
                    year = path[3];
                }
                if ( path.length > 4 )
                {
                    month = path[4];
                }
                if ( path.length > 5 )
                {
                    day = path[5];
                }

                ArticlesDataSource dataSource = new ArticlesDataSource();
                dataSource.searchByDate( year, month, day );
                renderArticles( dataSource );
            }
        }
        else
        {
            renderArticles( new ArticlesDataSource() );
        }
    }

    private void renderArticle( String id )
    {
        Article article = articleService.getArticleById( Long.valueOf( id ) );
        if ( article != null )
        {
            setHeader( article.getTitle() );
            add( new Paragraph( article.getContent() ) );
        }
        else
        {
            setHeader( localize( "header.notFound" ) );
            add( new Paragraph( "text.articleNotFound" ) );
        }
    }

    private void renderArticles( ArticlesDataSource dataSource )
    {
        setHeader( localize( "header.articles" ) );

        Repeater<Article> repeater = new Repeater<Article>( "articles-repeater", dataSource )
        {
            @Override
            public void populate( Article article )
            {
                add( new H2( article.getTitle() ) );
                add( new Paragraph( article.getContent() ) );
            }
        };

        add( repeater );
    }

    private class ArticlesDataSource
            implements IDataSource<Article>
    {
        private String year;

        private String month;

        private String day;

        private String category;

        private String tag;

        @Override
        public List<Article> getRows( int firstRow, int maxRows, String orderBy, OrderDirection direction )
        {
            ArticlesRequest request = new ArticlesRequest( firstRow, maxRows );

            request.addSort( orderBy, direction );
            request.setCategory( category );
            request.setTag( tag );
            request.setYear( year );
            request.setMonth( month );
            request.setDay( day );
            request.setPublished( true );

            return articleService.getArticles( request );
        }

        private void searchByCategory( String category )
        {
            this.category = category;
        }

        private void searchByTag( String tag )
        {
            this.tag = tag;
        }

        private void searchByDate( String year, String month, String day )
        {
            this.year = year;
            this.month = month;
            this.day = day;
        }
    }

    @Inject
    public void setArticleService( ArticleService articleService )
    {
        Articles.articleService = articleService;
    }
}